// pages/user/evaluate/evaluate.js
var api = require("../../../api.js"),
    app = getApp();
const handler=require("../../../utils/function.js")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        img: handler.userImg,
        driver: [],
        order: wx.getStorageSync("orderDetail"),
        satisfaction: ["满意", "一般", "不满意"],
        checkSatisfaction: -1,
        question: [],
        checkQuestion: [],
        idea: -1,
        order_no: '',
        graded: '',
        spendTime: 0, //服务时长
        distance: 0, //里程
        cid: 0, //里程
        display: "none",
        display_1: "none",
        display_2: "none",
        display_3: "none",
        display_4: "none",
        display_5: "none",
        xing: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no
        })
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
    },
    // satisfactionHandler 满意度选择
    satisfactionHandler: function(e) {
        this.setData({
            checkSatisfaction: e.currentTarget.dataset.id
        });
    },
    // questionHandler 存在问题选择
    questionHandler: function(e) {
        console.log('获取订单号', e)
        var checkQuestion = this.data.checkQuestion;
        var question = this.data.question;
        var id = e.currentTarget.dataset.id;
        for (var i = 0; i < question.length;i++){
          if(id==i){
            question[i].is_del = question[i].is_del==0?1:0
          }
        }
        checkQuestion.push(e.currentTarget.dataset.id);
        this.setData({
            checkQuestion: checkQuestion,
            question: question,
            cid: e.currentTarget.dataset.cid
        });
    },
    // ideaHandler 意见事件
    ideaHandler: function(e) {
        console.log('一键', e)
        this.setData({
            idea: e.detail.value
        });
    },
    // evaluateSubmitHandler 匿名提交事件
    evaluateSubmitHandler: function() {
        var that = this;
        var question = this.data.question;
        var cid=[];
        for (var i = 0; i < question.length;i++){
          if (question[i].is_del==1){
            cid.push(question[i].id)
          }
        }
        app.request({
            url: api.order_comment.order_comment,
            method: 'post',
            data: {
                satisfaction: that.data.xing,
                cid: JSON.stringify(cid.join(",")),
                idea: that.data.idea,
                driverid: that.data.driver.id,
                order_no: that.data.order_no,
            },
            success: function(rs) {
                console.log('意见提交', rs.data)
              if (rs.code==1){
                wx.showModal({
                  title: '提示',
                  content: rs.msg,
                  success: function (e) {
                    // wx.navigateBack({
                    //   delta: 1
                    // })
                    wx.navigateTo({
                      url: '/pages/map/driverServer/driverServer'
                    })
                  }
                })
              }else{
                wx.showToast({
                  title: rs.msg,
                  icon:'none'
                })
              }
            }
        })
    },
  //显示选中星星
  show_this: function (e) {
    var that = this;
    if (e.target.dataset.num == 1) {
      this.setData({
        display_1: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_1: "block",
        display_2: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 3) {
      this.setData({
        display_1: "block",
        display_2: "block",
        display_3: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 4) {
      this.setData({
        display_1: "block",
        display_2: "block",
        display_3: "block",
        display_4: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 5) {
      this.setData({
        display_1: "block",
        display_2: "block",
        display_3: "block",
        display_4: "block",
        display_5: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    console.log(e.target.dataset.num)
  },
  //取消选中星星
  hide_this: function (e) {
    var that = this;
    if (e.target.dataset.num == 5) {
      this.setData({
        display_1: "block",
        display_2: "block",
        display_3: "block",
        display_4: "block",
        display_5: "block"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 4) {
      this.setData({
        display_5: "none"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 3) {
      this.setData({
        display_4: "none",
        display_5: "none"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_3: "none",
        display_4: "none",
        display_5: "none"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
    if (e.target.dataset.num == 1) {
      this.setData({
        display_2: "none",
        display_3: "none",
        display_4: "none",
        display_5: "none"
      })
      that.setData({
        xing: e.target.dataset.num
      })
    };
  },

    // 验证提交信息
    testHandler: function(params, message) {
        if (params !== -1) {
            return true;
        } else {
            wx.showToast({
                title: message,
                icon: 'none',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        app.request({
            url: api.order_comment.getinfo,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
                console.log('sdsadsa', rs)
                if (rs.code == 1) {
                    that.setData({
                        question: rs.data.problem,
                        driver: rs.data.driver,
                        graded: rs.data.graded,
                        spendTime: rs.data.spendTime,
                        distance: rs.data.distance,
                    })
                  
                } else {
                  wx.showToast({
                      title: rs.msg,
                  })
                }
            }
        })
        this.setData({
            driver: wx.getStorageSync("driverDetail"),
            order: wx.getStorageSync("orderDetail")
        });
    },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})